package com.xframe.xdal.core.model.page;

/**
 * 功能描述:分页模型实现
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-26
 */
public class PagerModel<T> implements IPagerModel {

    /**
     * 数据
     */
    private T dataList;
    /**
     * 某一页
     */
    private int pageIndex;
    /**
     * 页大小
     */
    private int pageSize;
    /**
     * 总记录数
     */
    private Long recordCount;
    /**
     * 总页数
     */
    private Long pageCount;


    public PagerModel(int pageIndex, int pageSize) {
        init(pageIndex, pageSize);
    }



    /**
     * 初始化分页模型
     * @param pageIndex 开始页
     * @param pageSize 页大小
     */
    private void init(int pageIndex, int pageSize) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = 1;
        }
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    @Override
    public int getPageIndex() {
        return pageIndex;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public Long getRecordCount() {
        return recordCount;
    }

    @Override
    public void setRecordCount(Long recordCount) {
        this.recordCount = recordCount;
        if (this.recordCount <= 0) {
            this.recordCount = 0L;
        }
        if (recordCount > 0) {
            pageCount = recordCount % pageSize == 0 ?
                    recordCount / pageSize : recordCount / pageSize + 1;
        }
    }

    @Override
    public Long getPageCount() {
        return pageCount;
    }



    @Override
    public T getDataList() {
        return (T) this.dataList;
    }


    @Override
    public void setDataList(Object dataList) {
        this.dataList = (T) dataList;
    }


}
